<?php

class BugController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function createAction()
    {
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/bug/create');
        $bugReportForm->setMethod('post');
        if ($this->getRequest()->isPost()) {
        	if ($bugReportForm->isValid($_POST)) {
        		$bugModel = new Model_Bug();
        		// if the form is valid then create the new bug
        		$result = $bugModel->createBug($bugReportForm->getValue('author'),
        				$bugReportForm->getValue('email'),
        				$bugReportForm->getValue('date'),
        				$bugReportForm->getValue('url'),
        				$bugReportForm->getValue('description'),
        				$bugReportForm->getValue('priority'),
        				$bugReportForm->getValue('status')
        		);
        		// if the createBug method returns a result then the bug was successfully created
        		if ($result) {
        			$this->_forward('confirm');
        		}
        		}
        }
        $this->view->form = $bugReportForm;
    }

    public function confirmAction()
    {
        // action body
    }

    public function listAction()
    {
    	// get the filter form
    	$listToolsForm = new Form_BugReportListToolsForm();
    	$listToolsForm->setAction('/bug/list');
    	$listToolsForm->setMethod('post');
    	$this->view->listToolsForm = $listToolsForm;
    	
    	// set the default values to null
    	$sort = $this->_request->getParam('sort', null);
    	$filterField = $this->_request->getParam('filter_field', null);
    	$filterValue = $this->_request->getParam('filter');
    	
    	if (!empty($filterField)) {
    		$filter[$filterField] = $filterValue;
    	} else {
    		$filter = null;
    	}
    	
    	$listToolsForm->getElement('sort')->setValue($sort);
    	$listToolsForm->getElement('filter_field')->setValue($filterField);
    	$listToolsForm->getElement('filter')->setValue($filterValue);
    	
    	// fetch the bug paginator adapter
    	$bugModels = new Model_Bug();
    	$adapter = $bugModels->fetchPaginatorAdapter($filter, $sort);
    	$paginator = new Zend_Paginator($adapter);
    	// show 10 bugs per page
    	$paginator->setItemCountPerPage(10);
    	// get the page number that is passed in the request. if none is set then default to page 1
    	$page = $this->_request->getParam('page', 1);
    	$paginator->setCurrentPageNumber($page);
    	$this->view->paginator = $paginator;
    	
    }

    public function editAction()
    {
        $bugModel = new Model_Bug();
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/bug/edit');
        $bugReportForm->setMethod('post');
        if ($this->getRequest()->isPost()) {
        	if ($bugReportForm->isValid($_POST)) {
        		$bugModel = new Model_Bug();
        		// if the form is valid then update the bug
        		$result = $bugModel->updateBug(
        			$bugReportForm->getValue('id'),
        			$bugReportForm->getValue('author'),
        			$bugReportForm->getValue('email'),
        			$bugReportForm->getValue('date'),
        			$bugReportForm->getValue('url'),
        			$bugReportForm->getValue('description'),
        			$bugReportForm->getValue('priority'),
        			$bugReportForm->getValue('status')
        	);
        	return $this->_forward('list');
        	}
        } else {
        	$id = $this->_request->getParam('id');
        	$bug = $bugModel->find($id)->current();
        	$bugReportForm->populate($bug->toArray());
        	// format the date field
        	$bugReportForm->getElement('date')->setValue(date('d-m-Y', $bug->date));
        }
        $this->view->form = $bugReportForm;
    }

    public function deleteAction()
    {
        $bugModel = new Model_Bug();
        $id = $this->_request->getParam('id');
        $bugModel->deleteBug($id);
        return $this->_forward('list');
    }


}







